package services

import (
	"context"
	"database/sql"
	db "dhuviewer/db_query"
	"dhuviewer/dto"
	"fmt"

	"github.com/google/uuid"
	"github.com/labstack/gommon/log"
)

func CreateReply(tempReply *dto.Reply, uid uuid.UUID, username string) (uuid.UUID, error) {
	res, err := queries.CreateReply(context.Background(), db.CreateReplyParams{
		Gid:      tempReply.Gid,
		Title:    sql.NullString{String: tempReply.Title, Valid: true},
		Text:     sql.NullString{String: tempReply.Text, Valid: true},
		Uid:      uid,
		Username: sql.NullString{String: username, Valid: true},
		Images:   tempReply.Images,
	})
	if err != nil {
		log.Info("error:", err)
		return uuid.Nil, err
	}
	// fmt.Println(res)
	return res.Rid, nil
}

func DeleteReply(rid uuid.UUID) error {
	err := queries.DeleteReply(context.Background(), rid)
	return err
}

func DeleteReplySelf(rid uuid.UUID, uid uuid.UUID) error {
	err := queries.DeleteReplySelf(context.Background(), db.DeleteReplySelfParams{
		Rid: rid,
		Uid: uid,
	})
	return err
}

func DeleteReplyByGid(gid uuid.UUID) error {
	err := queries.DeleteReplyByGid(context.Background(), gid)
	return err
}

func DeleteReplyByUid(uid uuid.UUID) error {
	err := queries.DeleteReplyByUid(context.Background(), uid)
	return err
}

func ListReplyByGid(gid uuid.UUID) ([]db.Reply, error) {
	replyData, err := queries.ListReplyByGid(context.Background(), gid)
	if err != nil {
		log.Info("error:", err)
		return []db.Reply{}, err
	}
	return replyData, nil
}

func ListReplyByUid(uid uuid.UUID) ([]db.Reply, error) {
	replyData, err := queries.ListReplyByUid(context.Background(), uid)
	if err != nil {
		log.Info("error:", err)
		return []db.Reply{}, err
	}
	return replyData, nil
}

func ListReplyByRid(rid uuid.UUID) (db.Reply, error) {
	replyData, err := queries.ListReplyByRid(context.Background(), rid)
	if err != nil {
		log.Info("error:", err)
		return db.Reply{}, err
	}
	return replyData, nil
}

func StatReply(uid uuid.UUID) ([]db.StatDate, error) {
	replyStat, err := queries.StatReply(context.Background(), uid)
	if err != nil {
		return nil, err
	}
	return replyStat, nil
}

func StatCom(uid uuid.UUID) ([]db.StatUsername, error) {
	comStat, err := queries.StatComName(context.Background(), uid)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	return comStat, nil
}
